package com.ldy.core.web;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldy.core.common.Result;
import com.ldy.core.entity.BaseApp;
import com.ldy.core.service.BaseAppService;

/**
 * 应用管理
 */
@RestController
@RequestMapping("/app")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class AppController {
	
	private static final Logger logger = LoggerFactory.getLogger(AppController.class);
	
	@Autowired
	private BaseAppService appService;

	/**
	 * 列表分页查询
	 * @param current
	 * @param size
	 * @param appName
	 * @return
	 */
	@PostMapping("/selectPage")
	public Result selectPage(int current, int size, String appName) {
		Page<BaseApp> page = new Page<BaseApp>(current, size);
		QueryWrapper<BaseApp> query = new QueryWrapper<>();
		if (StringUtils.hasText(appName)) {
			query.like("app_name", appName);
		}
		query.orderByAsc("order_no").orderByDesc("create_time");
		return Result.success(appService.page(page, query));
	}
	
	/**
	 * 列表查询
	 * @return
	 */
	@PostMapping("/selectList")
	public Result selectList() {
		QueryWrapper<BaseApp> query = new QueryWrapper<>();
		query.orderByAsc("order_no").orderByDesc("create_time");
		return Result.success(appService.list(query));
	}
	
	/**
	 * 新增
	 * 
	 * @return
	 */
	@PostMapping("/insert")
	public Result insert(BaseApp app) {
		logger.info("method: {}, params:[app: {}]", "insert", JSONObject.toJSONString(app));
		Date now = new Date();
		app.setCreateTime(now);
		app.setLastUpdateTime(now);
		appService.save(app);
		return Result.success();
	}
	
	/**
	 * 更新
	 * 
	 * @return
	 */
	@PostMapping("/update")
	public Result update(BaseApp app) {
		logger.info("method: {}, params:[app: {}]", "update", JSONObject.toJSONString(app));
		Date now = new Date();
		app.setLastUpdateTime(now);
		appService.updateById(app);
		return Result.success();
	}
	
	/**
	 * 删除
	 * 
	 * @return
	 */
	@PostMapping("/delete")
	public Result delete(String id) {
		logger.info("method: {}, params:[id: {}]", "delete", id);
		appService.removeById(id);
		return Result.success();
	}
}
